package com.enqualcomm.kids.mvp.question;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.enqualcomm.kids.R;


/**
 * Created by Sunzhigang on 2016/3/8.
 */
public class ButtonRecycleViewAdapter extends RecyclerView.Adapter<ButtonRecycleViewAdapter.ViewHolder> {

    private LayoutInflater mInflater;
    private int selectedPosition;
    private String[] titles;

    public ButtonRecycleViewAdapter(Context context, String[] titles) {
        mInflater = LayoutInflater.from(context);
        this.titles = titles;
    }

    public void onItemSelected(int position) {
        int lastPosition = selectedPosition;
        selectedPosition = position;
        notifyItemChanged(lastPosition);
        notifyItemChanged(position);
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.button_list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.textView.setText(titles[position]);

//        if (position == 0 || position == 4) {
//            holder.view.setBackgroundResource(R.drawable.reai_question_btn_bg_left);
//        } else if (position == titles.length - 1 || position == 3) {
//            holder.view.setBackgroundResource(R.drawable.reai_question_btn_bg_right);
//        } else {
//            holder.view.setBackgroundResource(R.drawable.reai_question_btn_bg_middle);
//        }
        switch (position){
            case 0:
                holder.view.setBackgroundResource(R.drawable.q_button_one);
                break;
            case 1:
                holder.view.setBackgroundResource(R.drawable.q_button_two);
                break;
            case 2:
                holder.view.setBackgroundResource(R.drawable.q_button_three);
                break;
            case 3:
                holder.view.setBackgroundResource(R.drawable.q_button_four);
                break;
            case 4:
                holder.view.setBackgroundResource(R.drawable.q_button_five);
                break;
            case 5:
                holder.view.setBackgroundResource(R.drawable.q_button_six);
                break;
            case 6:
                holder.view.setBackgroundResource(R.drawable.q_button_seven);
                break;
            case 7:
                holder.view.setBackgroundResource(R.drawable.q_button_eight);
                break;
        }

        holder.view.setSelected(position == selectedPosition);
    }

    @Override
    public int getItemCount() {
        return titles == null ? 0 : titles.length;
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View arg0) {
            super(arg0);
            this.view = arg0;
            this.textView = (TextView) view.findViewById(R.id.button);
        }

        View view;
        TextView textView;
    }

}
